package com.longmao.formvalidate.controller;

import com.longmao.formvalidate.exception.ValidationException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.List;

public class BaseController {

    protected void validateForm(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> errors = bindingResult.getAllErrors();
            throw new ValidationException(400, errors.get(0).getDefaultMessage());
        }
    }

}
